import axios from "axios";
// 请求拦截器
axios.interceptors.request.use(config => {
    // Do something before request is sent
    // 获取token
    const token = localStorage.getItem("token")
    // 发请求的时候携带token
    config.headers.Authorization = `Bearer ${token}`
    // console.log(config.headers);
    return config;
}, error => {
    // Do something with request error
    return Promise.reject(error);
});


// 响应拦截器
axios.interceptors.response.use(response => {
    // Do something before response is sent
    // console.log(response.headers);
    // 统一设置token
    const { authorization } = response.headers
    authorization && localStorage.setItem('token', authorization)
    // console.log(authorization);
    // console.log(response);
    return response;
}, error => {
    // Do something with response error
    const { status } = error.response
    if (status === 401 ) {
        localStorage.removeItem("token")
        window.location.href = '#/login'
    }
    return Promise.reject(error);
});